﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;
using System.Configuration;

namespace QuanLyTruongHoc_App
{
    public partial class HocBa : Form
    {
        private DataTable data, dataXepLoai;
        private int state,
                    idHocSinh,
                    pow;

        private DataGridViewComboBoxColumn grc_HocLuc, grc_HanhKiem;
        private BindingSource binding;

        public HocBa(int _idHocSinh)
        {
            InitializeComponent();
            idHocSinh = _idHocSinh; 
        }

        private void checkUserPower()
        {
            pow = PublicLib.getUserPower(PConst.QUYEN_QL_HOC_BA);
            // user co quyen view
            
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            tsbt_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            //tsbt_Edit.Enabled = 
            cb_LenLop.Enabled = cb_Lop.Enabled = 
                tsb_Luu.Enabled = ((pow & PConst.POW_EDIT) > 0);

            tb_ChungChi.ReadOnly = tb_SoNgayNghi.ReadOnly = 
                rtb_KhenThuong.ReadOnly = rtb_NhanXet.ReadOnly = rtb_NXHieuTruong.ReadOnly =
                    dg_DanhSach.ReadOnly = dg_HK_HL.ReadOnly = 
                         !((pow & PConst.POW_EDIT) > 0);
            // user co delete
            //dg_DanhSach.AllowUserToDeleteRows =
            //    tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void reloadComboBox()
        {
            state = PConst.STATE_LOAD;
            // --------------
            // Nam Hoc
            MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter namAdap = new tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            namAdap.FillByHocBa(qLTH.tbl_NamHoc, idHocSinh);

            tscb_NamHoc.Items.Clear();
            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
            // --------------
            // load defaut data 
            tbl_GiaoVienTableAdapter gvAdap = new tbl_GiaoVienTableAdapter();
            tbl_MonHocTableAdapter monAdap = new tbl_MonHocTableAdapter();
            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            
            gvAdap.Connection.ConnectionString = 
                this.tbl_LopHocTableAdapter.Connection.ConnectionString = 
                    monAdap.Connection.ConnectionString =
                        hocbaAdap.Connection.ConnectionString =
                            tbl_LenLopTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();

            this.tbl_LopHocTableAdapter.Fill(this.qLTH.tbl_LopHoc);
            gvAdap.Fill(qLTH.tbl_GiaoVien);
            monAdap.Fill(qLTH.tbl_MonHoc);
            hocbaAdap.FillByHocSinh(qLTH.tbl_HocBa, idHocSinh);
            tbl_LenLopTableAdapter.Fill(this.qLTH.tbl_LenLop);

            // ---end load default data
            loadDataStruct();
            // Load ten hoc sinh
            tbl_HocSinhTableAdapter hsAdap = new tbl_HocSinhTableAdapter();
            hsAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hsAdap.FillByIdHocSinh(qLTH.tbl_HocSinh, idHocSinh);

            String result = "";

            if (qLTH.tbl_HocSinh.Rows.Count > 0)
            {
                result = PublicLib.formatHSId((int)qLTH.tbl_HocSinh.Rows[0][qLTH.tbl_HocSinh.idColumn]) + " - " +
                         qLTH.tbl_HocSinh.Rows[0][qLTH.tbl_HocSinh.HoDemColumn].ToString() + " " +
                         qLTH.tbl_HocSinh.Rows[0][qLTH.tbl_HocSinh.TenColumn].ToString();
            }

            tslb_TenHS.Text = result;
            this.Text += (" - " + result);

            cb_LenLop.SelectedIndex = -1;
            state = PConst.STATE_LOADED;
        }

        private void loadDataStruct()
        {
            // Diem ky hoc
            data = new DataTable();
            // Add Hoc Ky
            data.Columns.Add("Môn Học", Type.GetType("System.String"));
            data.Columns.Add("HK I", Type.GetType("System.Single"));
            data.Columns.Add("HK II", Type.GetType("System.Single"));
            data.Columns.Add("Cả Năm", Type.GetType("System.Single"));
            data.Columns.Add("Giáo Viên", Type.GetType("System.String"));
            // ---------
            dg_DanhSach.DataSource = data;

            dg_DanhSach.Columns[1].Width =
                dg_DanhSach.Columns[2].Width = 
                    dg_DanhSach.Columns[3].Width = 50;

            dg_DanhSach.Columns[4].Width = 150; // Ten Giao Vien
            // Xep loai
            dataXepLoai = new DataTable();
            dataXepLoai.Columns.Add("ten", Type.GetType("System.String"));
            dataXepLoai.Columns.Add("hocluc", Type.GetType("System.Int32"));
            dataXepLoai.Columns.Add("hanhkiem", Type.GetType("System.Int32"));

            binding = new BindingSource();
            dg_HK_HL.DataSource = binding;

            tbl_HocLucTableAdapter hlAdap = new tbl_HocLucTableAdapter();
            tbl_HanhKiemTableAdapter hkAdap = new tbl_HanhKiemTableAdapter();
            hlAdap.Connection.ConnectionString = hkAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hlAdap.FillByFlag(qLTH.tbl_HocLuc, PConst.FLAG_NONE);
            hkAdap.FillByFlag(qLTH.tbl_HanhKiem, PConst.FLAG_NONE);

            DataGridViewTextBoxColumn grc_Ten = new DataGridViewTextBoxColumn();
            grc_Ten.HeaderText = "";
            grc_Ten.Width = 150;
            grc_Ten.DataPropertyName = "ten";
            grc_Ten.ReadOnly = true;
            grc_Ten.SortMode = DataGridViewColumnSortMode.NotSortable;
            dg_HK_HL.Columns.Add(grc_Ten);

            grc_HocLuc = new DataGridViewComboBoxColumn();
            grc_HocLuc.HeaderText = "Học lực";
            grc_HocLuc.Width = 100;
            grc_HocLuc.DataSource = qLTH.tbl_HocLuc;
            grc_HocLuc.DisplayMember = qLTH.tbl_HocLuc.TenColumn.ColumnName;
            grc_HocLuc.ValueMember = qLTH.tbl_HocLuc.idColumn.ColumnName;
            grc_HocLuc.DataPropertyName = "hocluc";
            grc_HocLuc.SortMode = DataGridViewColumnSortMode.NotSortable;
            //grc_HocLuc.ReadOnly = true;
            dg_HK_HL.Columns.Add(grc_HocLuc);

            grc_HanhKiem = new DataGridViewComboBoxColumn();
            grc_HanhKiem.HeaderText = "Hạnh kiểm";
            grc_HanhKiem.Width = 100;
            grc_HanhKiem.DataSource = qLTH.tbl_HanhKiem;
            grc_HanhKiem.DisplayMember = qLTH.tbl_HanhKiem.TenColumn.ColumnName;
            grc_HanhKiem.ValueMember = qLTH.tbl_HanhKiem.idColumn.ColumnName;
            grc_HanhKiem.DataPropertyName = "hanhkiem";
            grc_HanhKiem.SortMode = DataGridViewColumnSortMode.NotSortable;
            dg_HK_HL.Columns.Add(grc_HanhKiem);

            binding.DataSource = dataXepLoai;
        }

        private void reloadDataTable()
        {
            // load cb_lop
            int idNamHoc = (int)qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            int idHKI, idHKII;
            #region Load idHocKy
            tbl_HocKyTableAdapter hocKyAdap = new tbl_HocKyTableAdapter();
            hocKyAdap.Connection.ConnectionString = PublicLib.getStrConn();
            hocKyAdap.FillByNamHoc(qLTH.tbl_HocKy, idNamHoc);
            if (qLTH.tbl_HocKy.Rows.Count < 2)
            {
                MessageBox.Show(MSG.MSG_ERROR_DB + qLTH.tbl_HocKy.TableName);
                return;
            }
            idHKI = (int)qLTH.tbl_HocKy.Rows[0][qLTH.tbl_HocKy.idColumn];
            idHKII = (int)qLTH.tbl_HocKy.Rows[1][qLTH.tbl_HocKy.idColumn];
            if (idHKI > idHKII)
            {
                int c = idHKI;
                idHKI = idHKII;
                idHKII = c;
            }

            qh_LopHocSinhTableAdapter lopHSAdap = new qh_LopHocSinhTableAdapter();
            lopHSAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopHSAdap.FillByNamHocHocSinh(qLTH.qh_LopHocSinh, idNamHoc, idHocSinh);

            if (qLTH.qh_LopHocSinh.Rows.Count == 0)
            {
                MessageBox.Show(MSG.MSG_ERROR_DB + " " + qLTH.qh_LopHocSinh.TableName);
                return;
            }
            // -----------------
            #endregion
            // Add Mon Hoc
            int idLop =       (int)qLTH.qh_LopHocSinh.Rows[0][qLTH.qh_LopHocSinh.idLopColumn];
            cb_Lop.SelectedValue = idLop;
            #region load data
            qh_HocSinhMonHocTableAdapter hsMHAdap = new qh_HocSinhMonHocTableAdapter();
            qh_LopMonHocTableAdapter lopMonHocAdap = new qh_LopMonHocTableAdapter();
            qh_GVDayTableAdapter gvDay = new qh_GVDayTableAdapter();
            lopMonHocAdap.Connection.ConnectionString = 
                hsMHAdap.Connection.ConnectionString = 
                    gvDay.Connection.ConnectionString = PublicLib.getStrConn();
            lopMonHocAdap.FillByLop(qLTH.qh_LopMonHoc, idLop);
            MyDataSet.QLTH.qh_HocSinhMonHocDataTable diem_HK1 = new QLTH.qh_HocSinhMonHocDataTable(),
                                                     diem_HK2 = new QLTH.qh_HocSinhMonHocDataTable();

            hsMHAdap.FillByHocSinhHocKy(diem_HK1, idHocSinh, (int)qLTH.tbl_HocKy[0][qLTH.tbl_HocKy.idColumn]);
            hsMHAdap.FillByHocSinhHocKy(diem_HK2, idHocSinh, (int)qLTH.tbl_HocKy[1][qLTH.tbl_HocKy.idColumn]);
            gvDay.FillByNamHocLop(qLTH.qh_GVDay, idNamHoc, idLop);
            #endregion 
            int dv = 0, dvKy1 = 0, dvKy2 = 0;
            // fix id mon toan la 1, mon van la 2
            float tbKy1 = 0, tbKy2 = 0,
                  toan = 0, van = 0, chuyen = -1, sum = 0, diemMin = 10000, 
                  toanKy1 = 0, vanKy1 = 0, chuyeKy1 = -1, minKy1 = 10000, sumKy1 = 0, 
                  toanKy2 = 0, vanKy2 = 0, chuyeKy2 = -1, minKy2 = 10000, sumKy2 = 0;

            int hlKy1, hlKy2, hlCaNam;
            data.Rows.Clear();
            for (int j = 0; j < qLTH.qh_LopMonHoc.Rows.Count; j++)
            {
                float diemCaNam = 0, hk1 = -1, hk2 = -1;
                object valKy1 = DBNull.Value, valKy2 = DBNull.Value;

                int idMonHoc = (int)qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn];
                #region load value
                int heso;
                if (qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.HeSoColumn] != DBNull.Value)
                    heso = (int)qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.HeSoColumn];
                else
                    heso = 1; // default he so mon hoc se la 1

                DataRow[] tmpRow1 = diem_HK1.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName + "=" + idMonHoc);
                DataRow[] tmpRow2 = diem_HK2.Select(qLTH.qh_HocSinhMonHoc.idMonHocColumn.ColumnName + "=" + idMonHoc);
                if (tmpRow1.Length > 0 && tmpRow2.Length > 0)
                {   // Mon duoc hoc trong ca 2 hoc ky
                    hk1 = (float)tmpRow1[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                    hk2 = (float)tmpRow2[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                    diemCaNam = (hk2 * 2 + hk1) / 3;
                    dv += heso;

                    sumKy1 += heso * hk1;
                    sumKy2 += heso * hk2;
                    dvKy1 += heso;
                    dvKy2 += heso;

                    if (hk1 < minKy1) minKy1 = hk1;
                    if (hk2 < minKy2) minKy2 = hk2;
                }
                else
                {
                    // chi 1 ky hoc
                    if (tmpRow1.Length > 0)
                    {
                        hk1 = (float)tmpRow1[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                        diemCaNam = hk1;
                        dv += heso;

                        sumKy1 += heso * hk1;
                        dvKy1 += heso;

                        if (hk1 < minKy1) minKy1 = hk1;
                    }

                    if (tmpRow2.Length > 0)
                    {
                        hk2 = (float)tmpRow2[0][qLTH.qh_HocSinhMonHoc.DiemTBColumn.ColumnName];
                        diemCaNam = hk2;
                        dv += heso;

                        sumKy2 += heso * hk2;
                        dvKy2 += heso;

                        if (hk2 < minKy2) minKy2 = hk2;
                    }

                }

                diemCaNam = float.Parse(diemCaNam.ToString(PConst.LAM_TRON)); // lam tron
                sum += heso * diemCaNam;

                if (diemCaNam < diemMin) diemMin = diemCaNam;
                if ((int)(qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn]) == 1)
                {
                    toan = diemCaNam;
                    toanKy1 = hk1;
                    toanKy2 = hk2;
                }
                if ((int)(qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.idMonColumn]) == 2)
                {
                    van = diemCaNam;
                    vanKy1 = hk1;
                    vanKy2 = hk2;
                }
                if (qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.MonChuyenColumn] != DBNull.Value)
                    if ((Boolean)qLTH.qh_LopMonHoc[j][qLTH.qh_LopMonHoc.MonChuyenColumn])
                    {
                        chuyen = diemCaNam;
                        chuyeKy1 = hk1;
                        chuyeKy2 = hk2;
                    }
                #endregion
                if (hk1 != -1) valKy1 = hk1;
                if (hk2 != -1) valKy2 = hk2;

                DataRow[] monRow = qLTH.tbl_MonHoc.Select(qLTH.tbl_MonHoc.idColumn.ColumnName + "=" + idMonHoc);
                String tenMon = (monRow.Length > 0) ? monRow[0][qLTH.tbl_MonHoc.TenMonHocColumn].ToString() : "";

                DataRow[] gvRow = qLTH.qh_GVDay.Select(qLTH.qh_GVDay.idMonHocColumn.ColumnName + "=" + idMonHoc);
                String tenGV = (gvRow.Length > 0) ? getTenGVFromID((int)gvRow[0][qLTH.qh_GVDay.idGiaoVienColumn]) : "";
                data.Rows.Add(tenMon, valKy1, valKy2, diemCaNam, tenGV);
            }

            tbKy1 = (dvKy1 > 0) ? sumKy1 / dvKy1 : 0;
            tbKy2 = (dvKy2 > 0) ? sumKy2 / dvKy2 : 0;
            float caNam = (dv > 0) ? sum / dv : 0;

            data.Rows.Add(MSG.MNU_ALL_YEAR, tbKy1.ToString(PConst.LAM_TRON), 
                                            tbKy2.ToString(PConst.LAM_TRON), 
                                            caNam.ToString(PConst.LAM_TRON));

            if (chuyen != -1)
            {
                hlKy1 = PublicLib.XetHocLuc_LopChuyen(chuyeKy1, minKy1, tbKy1);
                hlKy2 = PublicLib.XetHocLuc_LopChuyen(chuyeKy2, minKy2, tbKy2);
                hlCaNam = PublicLib.XetHocLuc_LopChuyen(chuyen, diemMin, caNam);
            }
            else
            {
                hlKy1 = PublicLib.XetHocLuc_LopThuong(toanKy1, vanKy1, minKy1, tbKy1);
                hlKy2 = PublicLib.XetHocLuc_LopThuong(toanKy2, vanKy2, minKy2, tbKy2);
                hlCaNam = PublicLib.XetHocLuc_LopThuong(toan, van, diemMin, caNam);
            }

            dataXepLoai.Rows.Clear();
            dataXepLoai.Rows.Add(MSG.MNU_HKI, hlKy1, DBNull.Value);
            dataXepLoai.Rows.Add(MSG.MNU_HKII, hlKy2, DBNull.Value);
            dataXepLoai.Rows.Add(MSG.MNU_ALL_YEAR, hlCaNam, DBNull.Value);
            dataXepLoai.Rows.Add(MSG.MNU_SAU_REN_LUYEN, DBNull.Value, DBNull.Value);

            /*dg_HK_HL[1, 0].ReadOnly = true;
            dg_HK_HL[1, 1].ReadOnly = true;
            dg_HK_HL[1, 2].ReadOnly = true;
            dg_HK_HL[2, 2].ReadOnly = true;*/

            DataRow[] hocBaRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idNamHocColumn.ColumnName + "=" + idNamHoc);
            if (hocBaRow.Length > 0)
            {
                if (hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column] != DBNull.Value &&
                    hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column] != DBNull.Value)
                {
                    int idHK1 = (int)hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column],
                        idHK2 = (int)hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column];

                    int idHKCaNam = (idHK1 + idHK2 * 2) / 3;
                    dataXepLoai.Rows[0][2] = idHK1;
                    dataXepLoai.Rows[1][2] = idHK2;
                    dataXepLoai.Rows[2][2] = idHKCaNam;
                }
                else
                {
                    dataXepLoai.Rows[0][2] = hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column];
                    dataXepLoai.Rows[1][2] = hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column];

                    if (dataXepLoai.Rows[0][2] != DBNull.Value) dataXepLoai.Rows[2][2] = dataXepLoai.Rows[0][2];
                    if (dataXepLoai.Rows[1][2] != DBNull.Value) dataXepLoai.Rows[2][2] = dataXepLoai.Rows[1][2];
                    
                }

                if (hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn] != DBNull.Value)
                    dataXepLoai.Rows[2][2] = hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn];
                // Hoc Luc
                if (hocBaRow[0][qLTH.tbl_HocBa.HocLucKy1Column] != DBNull.Value)
                    dataXepLoai.Rows[0][1] = hocBaRow[0][qLTH.tbl_HocBa.HocLucKy1Column];
                if (hocBaRow[0][qLTH.tbl_HocBa.HocLucKy2Column] != DBNull.Value)
                    dataXepLoai.Rows[1][1] = hocBaRow[0][qLTH.tbl_HocBa.HocLucKy2Column];
                if (hocBaRow[0][qLTH.tbl_HocBa.HocLucCNColumn] != DBNull.Value)
                    dataXepLoai.Rows[2][1] = hocBaRow[0][qLTH.tbl_HocBa.HocLucCNColumn];

                dataXepLoai.Rows[3][1] = hocBaRow[0][qLTH.tbl_HocBa.HLSauRenLuyenColumn];
                dataXepLoai.Rows[3][2] = hocBaRow[0][qLTH.tbl_HocBa.HKSauRenLuyenColumn];

                tb_SoNgayNghi.Text = hocBaRow[0][qLTH.tbl_HocBa.SoNgayNghiColumn].ToString();
                if (hocBaRow[0][qLTH.tbl_HocBa.idLenLopColumn] !=  DBNull.Value)
                    cb_LenLop.SelectedValue = (int)hocBaRow[0][qLTH.tbl_HocBa.idLenLopColumn];
                tb_ChungChi.Text = hocBaRow[0][qLTH.tbl_HocBa.ChungChiNgheColumn].ToString();
                rtb_KhenThuong.Text = hocBaRow[0][qLTH.tbl_HocBa.ChungChiNgheColumn].ToString();
                rtb_NhanXet.Text = hocBaRow[0][qLTH.tbl_HocBa.NhanXetGVCNColumn].ToString();
                rtb_NXHieuTruong.Text = hocBaRow[0][qLTH.tbl_HocBa.NhanXetHieuTruongColumn].ToString();
            }
            //fillData();
        }

        private void fillData()
        {
            if (state == PConst.STATE_LOAD) return;
            calculateData();
        }

        private void refreshData()
        {
            reloadComboBox();
            reloadDataTable();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width =
                grb_ThongTin.Width =
                    dg_HK_HL.Width = (this.Width - 22) / 2;

            grb_ThongTin.Left = dg_DanhSach.Right + 5;

            grb_ThongTin.Height = stt_Bottom.Top - dg_DanhSach.Top - 1;
            dg_HK_HL.Top = grb_ThongTin.Bottom - dg_HK_HL.Height;
            lb_XepLoai.Top = dg_HK_HL.Top - lb_XepLoai.Height - 5;
            dg_DanhSach.Height = lb_XepLoai.Top - dg_DanhSach.Top - 5;
            // control trong grb thong tin
            rtb_KhenThuong.Width = grb_ThongTin.Width - 10;
            rtb_NhanXet.Width = rtb_NXHieuTruong.Width = (grb_ThongTin.Width - 14) / 2;
            rtb_NXHieuTruong.Left = rtb_NhanXet.Right + 1;

            cb_LenLop.Width = rtb_KhenThuong.Right - cb_LenLop.Left;
            tb_ChungChi.Width = rtb_KhenThuong.Right - tb_ChungChi.Left;

            lb_NhanXetHT.Left = rtb_NXHieuTruong.Left;
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD)
                return;

            fillData();
        }

        private void calculateData()
        {
            if (dataXepLoai.Rows[0][2] != DBNull.Value && dataXepLoai.Rows[1][2] != DBNull.Value)
            {
                int idHK1 = (int)dataXepLoai.Rows[0][2],
                    idHK2 = (int)dataXepLoai.Rows[1][2];

                int idHKCaNam = (idHK1 + idHK2 * 2) / 3;
                dataXepLoai.Rows[2][2] = idHKCaNam;
            }
            else
            {
                if (dataXepLoai.Rows[0][2] != DBNull.Value) dataXepLoai.Rows[2][2] = dataXepLoai.Rows[0][2];
                if (dataXepLoai.Rows[1][2] != DBNull.Value) dataXepLoai.Rows[2][2] = dataXepLoai.Rows[1][2];
            }
        }

        private void tscb_NamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD) return;
            if (tscb_NamHoc.SelectedIndex == -1) return;
            reloadDataTable();
        }

        private String getTenGVFromID(int _id)
        {
            DataRow[] tmpRow = qLTH.tbl_GiaoVien.Select(qLTH.tbl_GiaoVien.idColumn.ColumnName + "=" + _id);
            return (tmpRow.Length > 0) ? tmpRow[0][qLTH.tbl_GiaoVien.HoDemColumn].ToString() + " " +
                                         tmpRow[0][qLTH.tbl_GiaoVien.TenColumn].ToString()
                                         : "";
        }

        private void cb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD || cb_Lop.SelectedIndex == -1) return;
            int idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            int idLop = (int)cb_Lop.SelectedValue;

            qh_GVChuNhiemTableAdapter qhCNAdap = new qh_GVChuNhiemTableAdapter();
            qhCNAdap.Connection.ConnectionString = PublicLib.getStrConn();

            qhCNAdap.FillByNamHocLop(qLTH.qh_GVChuNhiem, idNamHoc, idLop);
            if (qLTH.qh_GVChuNhiem.Rows.Count > 0)
            {
                int idGV = (int)qLTH.qh_GVChuNhiem[0][qLTH.qh_GVChuNhiem.idGiaoVienColumn];
                tb_TenGV.Text = getTenGVFromID(idGV);
            }
        }

        private void dg_HK_HL_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            calculateData();
        }

        private void saveData()
        {
            tbl_HocBaTableAdapter hocbaAdap = new tbl_HocBaTableAdapter();
            hocbaAdap.Connection.ConnectionString = PublicLib.getStrConn();

            hocbaAdap.FillByHocSinh(qLTH.tbl_HocBa, idHocSinh);
            int idNamHoc = (int)qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];

            DataRow[] hocBaRow = qLTH.tbl_HocBa.Select(qLTH.tbl_HocBa.idNamHocColumn.ColumnName + "=" + idNamHoc);
            if (hocBaRow.Length > 0)
            {
                int soNgayNghi = 0;
                if (!(tb_SoNgayNghi.Text == ""))
                    if (!int.TryParse(tb_SoNgayNghi.Text, out soNgayNghi))
                    {
                        MessageBox.Show(MSG.MSG_NHAP_SO);
                        return;
                    }
                hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK1Column] = dataXepLoai.Rows[0][2]; // Hanh kiem ky 1
                hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemHK2Column] = dataXepLoai.Rows[1][2]; // hanh kiem ky 2
                hocBaRow[0][qLTH.tbl_HocBa.idHanhKiemCNColumn] = dataXepLoai.Rows[2][2]; // hanh kiem Ca Nam
                // Hoc luc
                hocBaRow[0][qLTH.tbl_HocBa.HocLucKy1Column] = dataXepLoai.Rows[0][1]; // Hoc Luc ky 1
                hocBaRow[0][qLTH.tbl_HocBa.HocLucKy2Column] = dataXepLoai.Rows[1][1]; // Hoc luc ky 2
                hocBaRow[0][qLTH.tbl_HocBa.HocLucCNColumn] = dataXepLoai.Rows[2][1]; // hoc luc Ca Nam

                hocBaRow[0][qLTH.tbl_HocBa.HLSauRenLuyenColumn] = dataXepLoai.Rows[3][1]; // Hoc luc sau ren luyen
                hocBaRow[0][qLTH.tbl_HocBa.HKSauRenLuyenColumn] = dataXepLoai.Rows[3][2]; // hanh kiem sau ren luyen

                hocBaRow[0][qLTH.tbl_HocBa.SoNgayNghiColumn] = soNgayNghi;
                hocBaRow[0][qLTH.tbl_HocBa.idLenLopColumn] = cb_LenLop.SelectedValue;
                hocBaRow[0][qLTH.tbl_HocBa.ChungChiNgheColumn] = tb_ChungChi.Text;
                hocBaRow[0][qLTH.tbl_HocBa.KhenThuongColumn] = rtb_KhenThuong.Text;
                hocBaRow[0][qLTH.tbl_HocBa.NhanXetGVCNColumn] = rtb_NhanXet.Text;
                hocBaRow[0][qLTH.tbl_HocBa.NhanXetHieuTruongColumn] = rtb_NXHieuTruong.Text;

                hocbaAdap.Update(hocBaRow[0]);
            }
            else
                if (tscb_NamHoc.SelectedIndex == tscb_NamHoc.Items.Count - 1)
                {
                    DataRow nRow = qLTH.tbl_HocBa.NewRow();
                    int soNgayNghi = 0;
                    if (!(tb_SoNgayNghi.Text == ""))
                        if (!int.TryParse(tb_SoNgayNghi.Text, out soNgayNghi))
                        {
                            MessageBox.Show(MSG.MSG_NHAP_SO);
                            return;
                        }
                    nRow[qLTH.tbl_HocBa.idHocSinhColumn] = idHocSinh;
                    nRow[qLTH.tbl_HocBa.idNamHocColumn] = idNamHoc;

                    nRow[qLTH.tbl_HocBa.idHanhKiemHK1Column] = dataXepLoai.Rows[0][2]; // Hanh kiem ky 1
                    nRow[qLTH.tbl_HocBa.idHanhKiemHK2Column] = dataXepLoai.Rows[1][2]; // hanh kiem ky 2
                    nRow[qLTH.tbl_HocBa.idHanhKiemCNColumn] = dataXepLoai.Rows[2][2]; // hanh kiem Ca Nam
                    // Hoc luc
                    nRow[qLTH.tbl_HocBa.HocLucKy1Column] = dataXepLoai.Rows[0][1]; // Hoc Luc ky 1
                    nRow[qLTH.tbl_HocBa.HocLucKy2Column] = dataXepLoai.Rows[1][1]; // Hoc luc ky 2
                    nRow[qLTH.tbl_HocBa.HocLucCNColumn] = dataXepLoai.Rows[2][1]; // hoc luc Ca Nam

                    nRow[qLTH.tbl_HocBa.HLSauRenLuyenColumn] = dataXepLoai.Rows[3][1]; // Hoc luc sau ren luyen
                    nRow[qLTH.tbl_HocBa.HKSauRenLuyenColumn] = dataXepLoai.Rows[3][2]; // Hanh kiem sau ren luyen

                    nRow[qLTH.tbl_HocBa.SoNgayNghiColumn] = soNgayNghi;
                    if (cb_LenLop.SelectedIndex != -1) 
                        nRow[qLTH.tbl_HocBa.idLenLopColumn] = cb_LenLop.SelectedValue;
                    else
                        nRow[qLTH.tbl_HocBa.idLenLopColumn] = DBNull.Value;

                    nRow[qLTH.tbl_HocBa.ChungChiNgheColumn] = tb_ChungChi.Text;
                    nRow[qLTH.tbl_HocBa.KhenThuongColumn] = rtb_KhenThuong.Text;
                    nRow[qLTH.tbl_HocBa.NhanXetGVCNColumn] = rtb_NhanXet.Text;
                    nRow[qLTH.tbl_HocBa.NhanXetHieuTruongColumn] = rtb_NXHieuTruong.Text;

                    hocbaAdap.Update(hocBaRow[0]);
                }
        }

        private void tsb_Luu_Click(object sender, EventArgs e)
        {
            dg_HK_HL.EndEdit();
            saveData();
        }

        private void tsbt_NamTruoc_Click(object sender, EventArgs e)
        {
            if (tscb_NamHoc.SelectedIndex > 0)
            {
                tscb_NamHoc.SelectedIndex--;
            }
        }

        private void tsbt_NamToi_Click(object sender, EventArgs e)
        {
            if (tscb_NamHoc.SelectedIndex < tscb_NamHoc.Items.Count - 1)
            {
                tscb_NamHoc.SelectedIndex++;
            }
        }

        private void tsbt_TaoMoi_Click(object sender, EventArgs e)
        {
            MyControls.ThemTrangHocBa frm_ThemTrang = new QuanLyTruongHoc_App.MyControls.ThemTrangHocBa();
            frm_ThemTrang.MdiParent = this.MdiParent;
            frm_ThemTrang.Show();
            frm_ThemTrang.OkClicked += new QuanLyTruongHoc_App.MyControls.ThemTrangHocBa.EventUpdate(frm_ThemTrang_OkClicked);
        }

        private void frm_ThemTrang_OkClicked(Object sender, int _idLop, int _idNamHoc)
        {
            qh_LopHocSinhTableAdapter lopAdap = new qh_LopHocSinhTableAdapter();
            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();

            lopAdap.FillByHocSinh(qLTH.qh_LopHocSinh, idHocSinh);
            if (qLTH.qh_LopHocSinh.Select(qLTH.qh_LopHocSinh.idNamHocColumn.ColumnName + "=" + _idNamHoc).Length > 0)
            {
                MessageBox.Show(MSG.MSG_DA_CO_TRONG_HOC_BA);
                return;
            }
            else
            {
                DataRow nRow = qLTH.qh_LopHocSinh.NewRow();

                nRow[qLTH.qh_LopHocSinh.idHocSinhColumn] = idHocSinh;
                nRow[qLTH.qh_LopHocSinh.idNamHocColumn] = _idNamHoc;
                nRow[qLTH.qh_LopHocSinh.idLopColumn] = _idLop;

                qLTH.qh_LopHocSinh.Rows.Add(nRow);

                lopAdap.Update(qLTH.qh_LopHocSinh);

                refreshData();
            }
        }

        private void HocBa_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }
    }
}
